<?php

class MoneyController extends MController
{
	public function actionAddMoney()
	{
		$request = array();
		$params = array('userid', 'howmuch', 'ordernum');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		if (!$request['userid']) {
			$this->return_json(1, '用户ID不能为空');
		}
		$data = array(
			'uid' => $request['userid'],
			'paytype' => '在线充值',
			'howmuch' => $request['howmuch'],
			'ordernum' => $request['ordernum'],
			'createtime' => time()
		);
		$this->model('Financedetail')->firstAdd($request['userid'], 98);
		if ($this->model('Financedetail')->insert($data) !== false) {
			$this->model('UserMoney')->addMoney($request['userid'], $request['howmuch']);
			$this->return_json(0, '成功');
		}
		$this->return_json(1, '失败');
	}
	
	public function actionAddmoneylist()
	{
		$userid = $this->getRequest('userid');
		$list = $this->model('Financedetail')->getListByUserId($userid);
		$type = $this->getRequest('type', 0);
		$result['code'] = 0;
		$result['msg'] = '成功';
		if (!empty($list)) {
			foreach ($list as $i => $item) {
				$list[$i]['createtime'] = date('m-d H:i', $item['createtime']);
			}
		}
		$result['consumelist'] = $list;
		echo json_encode($result);die;
	}
	
	/**
	 * @deprecated
	 */
	public function actionConsumelist()
	{
		$userid = $this->getRequest('userid', 0);
		$type = $this->getRequest('type', 0);
		$consume = $this->model('consume')->getMyConsume($userid, $type);
		if (!empty($consume)) {
			foreach ($consume as $i => $item) {
				$consume[$i]['createtime'] = date('m-d H:i');
				if (in_array($item['consumetype'], array(1,2,3))) {
					$consume[$i]['howmuch'] = '-' . $item['howmuch'];
				} elseif ($item['consumetype'] == 4) {
					$consume[$i]['howmuch'] = '+' . $item['howmuch'];
				}
				switch ($item['consumetype']) {
					case '1':
						$consume[$i]['itemname'] = '购买了' . $item['itemname'];
						break;
					case '2':
						$consume[$i]['itemname'] = '订阅了用户';
						break;
					case '3':
						$consume[$i]['itemname'] = '支持了女神';
						break;
					case '4':
						$consume[$i]['itemname'] = '购买开币';
				}
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['consumelist'] = $consume;
		echo json_encode($result);die;
	}
	
	public function actionJifenlist()
	{
		$userid = $this->getRequest('userid');
		$consume = $this->model('jifen')->getListByUserId($userid);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['consumelist'] = $consume;
		echo json_encode($result);die;
	}
	
	public function actionBalance()
	{
		$userid = $this->getRequest('userid');
		$money = $this->model('UserMoney')->findByPk($userid);
		$jifen = $this->model('jifen')->getRecord($userid);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['balance'] = $money['howmuch'];
		$result['jifen'] = $jifen;
		echo json_encode($result);die;
	}
	
	public function actionConsume()
	{
		$request = array();
		$params = array('userid', 'howmuch', 'itemid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		$usermoney = (int) $this->model('UserMoney')->getMoney($request['userid']);
		if ($usermoney < $request['howmuch']) {
			$this->return_json(1, '余额不足');
		}
		$post = array(
			'userid' => (int) $request['userid'],
			'howmuch' => (int) $request['howmuch'],
			'itemid' => (int) $request['itemid'],
			'consumetype' => 1,
			'createtime' => time()
		);
		if ($this->model('consume')->insert($post) !== false) {
			$this->model('UserMoney')->updateMoney($request['userid'], $request['howmuch']);
			$this->return_json(0, '成功');
		}
		$this->return_json(1, '失败');
	}
	
	public function actionSupport()
	{
		$request = array();
		$params = array('userid', 'itemid', 'replyerid', 'howmuch');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}

		if ($request['userid'] == $request['replyerid']) {
			$this->return_json(1, '失败');
		}
		
		$usermoney = (int) $this->model('UserMoney')->getMoney($request['replyerid']);
		if ($usermoney < $request['howmuch']) {
			$this->return_json(1, '余额不足');
		}
	
		$data = array(
			'replyerid' => $request['replyerid'],
			'content' => '',
			'itemid' => (int) $request['itemid'],
			'userid' => (int) $request['userid'],
			'howmuch' => (int) $request['howmuch'],
			'replytype' => 1,
			'replytime' => time(),
			'picurl' => ''
		);
		if ($this->model('reply')->insert($data) !== false) {
			$post = array(
				'userid' => (int) $request['replyerid'],
				'howmuch' => (int) $request['howmuch'],
				'itemid' => (int) $request['itemid'],
				'sorpuserid' => (int) $request['userid'],
				'consumetype' => 3,
				'createtime' => time()
			);
			if ($this->model('consume')->insert($post) !== false) {
				$this->model('UserMoney')->updateMoney($request['replyerid'], $request['howmuch']);
				if ($request['userid'] > 0) {
					$this->model('UserMoney')->addMoney($request['userid'], $request['howmuch']);
				}
				if ($request['itemid'] > 0) {
					$this->model('item')->addMoney($request['itemid'], $request['howmuch']);
				}
			}
			$this->return_json(0, '成功');
		}
		$this->return_json(1, '失败');
	}
}